package com.wk.controller.teacher;

import com.wk.pojo.Class;
import com.wk.pojo.Stu_map;
import com.wk.pojo.Tea_map;
import com.wk.pojo.User;
import com.wk.service.Mapservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: thesis
 * @description: 老师设定签到类
 * @author: 王侃
 * @create: 2020-03-05 22:18
 **/
@Controller
@RequestMapping("/tea")
public class Teamapcontroller {

    @Autowired
    Mapservice mapservice;

    //接受Date类型数据
    @InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

    //跳转到设置签到界面
    @RequestMapping("/map/setting")
    public String map_set(Model model)
    {
        List<Class> classes=mapservice.selectbyallclass();
        model.addAttribute("classes",classes);
        return "teacher/map/location_setting";
    }

    //教师提交地点到数据库中
    @RequestMapping("/map/setting/submit")
    public String map_submit(Tea_map map, String location_all, HttpSession session)
    {

        User user=(User)session.getAttribute("loginuser");
        String location1=location_all.substring(0,location_all.indexOf(","));
        //截取逗号之前的为经度，逗号之后的为维度
        String location2=location_all.substring(location_all.lastIndexOf(",")+1);

        map.setLocation_1(location1);
        map.setLocation_2(location2);
        map.setUser_id(user.getId());
        map.setUsername(user.getUsername());

        mapservice.insertintoTea_map(map);
        return "common/success";
    }

    //跳转到学生签到情况的界面
    @RequestMapping("/map/student/record")
    public String map_record(HttpSession session,Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        List<Stu_map> maps=mapservice.selectbyteaid_stumap(user.getId());
        List<Tea_map> tea_maps=mapservice.selectbyteaid_teamap(user.getId());

        model.addAttribute("tea_maps",tea_maps);
        model.addAttribute("maps",maps);
        return "teacher/map/record";
    }

    //跳转到教师自己发布的签到信息
    @RequestMapping("/map/teacher/record")
    public String map_teacher_record(HttpSession session,Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        List<Tea_map> maps=mapservice.selectbyteaid_teamap(user.getId());

        model.addAttribute("maps",maps);
        return "teacher/map/tea_record";
    }
}
